﻿using System;
using System.Drawing;

namespace Segmentation.Business
{
    public class ImageContainer : IDisposable
    {
        #region Delegates

        public delegate void AddNoiseDelegate(double psnr);
        public delegate void SegmentDelegate(JSEGAccessor.Mode mode);

        #endregion Delegates

        #region Fields

        private Bitmap bitmap;
        private JSEGAccessor accessor;
        private double psnr;

        #endregion

        #region Constructors

        public ImageContainer(Bitmap bitmap)
        {
            this.bitmap = bitmap;
            this.accessor = new JSEGAccessor();
        }

        #endregion

        #region Properties

        public Bitmap Bitmap
        {
            get
            {
                return this.bitmap;
            }
        }

        public JSEGAccessor JSEGAccessor
        {
            get
            {
                return this.accessor;
            }
        }

        public double PSNR
        {
            get
            {
                return this.psnr;
            }
        }

        #endregion

        #region Public Methods

        public void BeginAddNoise(double psnr, AsyncCallback callback)
        {
            AddNoiseDelegate method = new AddNoiseDelegate(this.AddNoise);
            method.BeginInvoke(psnr, callback, null);
        }

        public void BeginSegment(JSEGAccessor.Mode mode, AsyncCallback callback)
        {
            SegmentDelegate method = new SegmentDelegate(this.Segment);
            method.BeginInvoke(mode, callback, null);
        }

        public void AddNoise(double psnr)
        {
            this.bitmap = GaussianNoise.AddNoise(this.bitmap, psnr);
            this.psnr = psnr;
        }

        public void Segment(JSEGAccessor.Mode mode)
        {
            this.accessor.Process(this.bitmap, mode);
        }

        #endregion
        
        #region IDisposable Members

        public void Dispose()
        {
            if (this.bitmap != null)
            {
                this.bitmap.Dispose();
            }
        }

        #endregion
    }
}
